package com.tywj.buscustomerapp.view.activity;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.model.bean.MyTicketBean;
import com.tywj.buscustomerapp.model.bean.TravelBean;
import com.tywj.buscustomerapp.utils.SPUtils;
import com.tywj.buscustomerapp.utils.WebServicrUtils;
import com.tywj.buscustomerapp.view.myview.TaylorStarRatingBar;
import com.tywj.buscustomerapp.view.myview.ToastJoe;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class EvaluateActivity extends BaseActivity {
    @BindView(R.id.head_title)
    TextView title;
    @BindView(R.id.recycler)
    GridView recyclerView;
    @BindView(R.id.taylorStarRatingBar)
    TaylorStarRatingBar ratingBar;
    @BindView(R.id.textView6)
    TextView textView6;
    @BindView(R.id.editText)
    EditText editText;
    @BindView(R.id.textView7)
    TextView submit;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.textView2)
    TextView textView2;
    @BindView(R.id.textView3)
    TextView textView3;
    @BindView(R.id.textView4)
    TextView textView4;
    @BindView(R.id.head_left)
    ImageView back;
    @BindView(R.id.head_right)
    TextView right;
    private SimpleAdapter adapter;
    private List<String> list1= new ArrayList<>();
    private List<String> list2 = new ArrayList<>();
    private List<Map<String, Object>> list=new ArrayList<>();
    private final String[] strings1 = {"服务态度恶劣","道路不熟","司机吸烟","车内有异味","行驶中玩手机","司机迟到"};
    private final String[] strings2 = {"车内整洁","驾驶平稳","活地图认路准","服务周到","价格实惠","暖心的微笑"};
    private final String[] strings3 = {"您的评价会让胖哒做的更好哦~","非常不满意，各方面都很差","不满意比较差","一般，需改善","比较满意，整体不错","非常满意，无可挑剔"};
    private List<String> list3 = new ArrayList<>();
    private boolean isLow = true;
    private TravelBean.DataBean.ResultListBean myTicketBean;
    private int score = 0;

    @Override
    protected int getContentId() {
        return R.layout.activity_evaluate;
    }


    @Override
    protected void initWidget(Bundle savedInstanceState) {
        super.initWidget(savedInstanceState);
        title.setText("评价");
        right.setText("投诉");
        if (getIntent()!=null){
            myTicketBean = (TravelBean.DataBean.ResultListBean) getIntent().getSerializableExtra("ticket");
            textView.setText(myTicketBean.getScsj());
            textView2.setText(myTicketBean.getXlmc());
            textView3.setText(myTicketBean.getGmqsz()+"-"+myTicketBean.getGmzdz());
            if (myTicketBean.getTicketType()==0)
                textView4.setText("￥"+myTicketBean.getPj());
            else if (myTicketBean.getTicketType()==1){
                textView4.setText("月票");
            }
        }
        for (String name:strings2) {
            list1.add(name);
        }
        for (String name:strings1) {
            list2.add(name);
        }
        getDate(list1);
        adapter=new SimpleAdapter(this,list,R.layout.item_eva,new String[]{"name"},
                new int[]{R.id.radio});
        recyclerView.setAdapter(adapter);
        recyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView textView = view.findViewById(R.id.radio);
                if (list3.contains(position+"")){
                    list3.remove(position+"");
                    textView.setTextColor(view.getContext().getResources().getColor(R.color.deepgray));
                    textView.setBackgroundResource(R.drawable.bg_text_normal);
                }else{
                    list3.add(position+"");
                    textView.setTextColor(view.getContext().getResources().getColor(R.color.text));
                    textView.setBackgroundResource(R.drawable.bg_text_selected);
                }
            }
        });
    }

    private void getDate(List<String> list1){
        for (int i = 0; i < list1.size(); i++){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", list1.get(i));
            list.add(map);
        }
    }

    @Override
    protected void initClick() {
        super.initClick();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EvaluateActivity.this,ComplaintActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("ticket", myTicketBean);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = "";
                for (int i = 0; i < list3.size(); i++) {
                    if (isLow){
                        message = list2.get(Integer.valueOf(list3.get(i))) + "," +message;
                    }else{
                        message = list1.get(Integer.valueOf(list3.get(i)))+ "," +message;
                    }
                }
                message = message + editText.getText().toString();

                if (score==0 || message.equals("")){
                    ToastJoe.getmToastJoe().ToastLongShow(EvaluateActivity.this,null,"请为我们的服务评价");
                }else{
                    doAppline(score,message.replace(",","，"));
                }

            }
        });
        ratingBar.setOnRatingListener(new TaylorStarRatingBar.RatingListener() {
            @Override
            public void OnRatingChanage(float star) {
                list3.clear();
                list.clear();
                adapter=new SimpleAdapter(EvaluateActivity.this,list,R.layout.item_eva,new String[]{"name"},
                        new int[]{R.id.radio});
                recyclerView.setAdapter(adapter);
                score = (int)star;
                if (score<5)
                    textView6.setText(strings3[score]);
                if (score>3){
                    isLow = false;
                    getDate(list1);
                    adapter.notifyDataSetChanged();
                }else{
                    isLow = true;
                    getDate(list2);
                    adapter.notifyDataSetChanged();
                }

            }
        });
    }

    private void doAppline(final int sco, final String message){
        Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter e){
                try {
                    String msg = WebServicrUtils.getInstance()
                            .postEva(myTicketBean.getCh(),sco+"",message,SPUtils.getUserPhone(getApplicationContext()));
                    if (msg.equals("ok")){
                        e.onNext(msg);
                    }else{
                        e.onError(null);
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                    e.onError(null);
                } catch (XmlPullParserException e1) {
                    e1.printStackTrace();
                    e.onError(null);
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Object o) {
                        ToastJoe.getmToastJoe().ToastShowRight(EvaluateActivity.this,null,"评价成功");
                    }


                    @Override
                    public void onError(Throwable e) {
                        ToastJoe.getmToastJoe().ToastShowRight(EvaluateActivity.this,null,"上传失败");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
